package com.cp.core.controller.admin;


import com.cp.base.util.JwtUtils;
import com.cp.common.result.R;
import com.cp.core.pojo.entity.BorrowInfo;
import com.cp.core.pojo.vo.BorrowInfoApprovalVO;
import com.cp.core.service.BorrowInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author ming
 * @since 2022-07-27
 */
@RestController
@Api(tags = "借款信息")
@RequestMapping("/admin/core/borrowInfo")
public class AdminBorrowInfoController {
    @Resource
    private BorrowInfoService borrowInfoService;
    @GetMapping("/auth/getBorrowAmount")
    public R getBorrowAuthAmount(HttpServletRequest request){
        Long userId = JwtUtils.getUserId(request.getHeader("token"));
        BigDecimal integral = borrowInfoService.getAmountByUserId(userId);
        return R.ok().data("integral",integral);
    }
    @PostMapping("/auth/save")
    public R save(@RequestBody BorrowInfo borrowInfo,HttpServletRequest request){
        Long userId = JwtUtils.getUserId(request.getHeader("token"));
        borrowInfoService.saveBorrowerInfo(borrowInfo,userId);
        return R.ok().message("提交成功");
    }
    @GetMapping("/auth/getBorrowerInfoStatus")
    public R getBorrowerInfoStatus(HttpServletRequest request){
        Long userId = JwtUtils.getUserId(request.getHeader("token"));
        return R.ok().data("borrowerInfoStatus",borrowInfoService.getBorrowerInfoStatusByUserId(userId));
    }
    @ApiOperation("借款人列表")
    @GetMapping("/list")
    public R list(){
        List<BorrowInfo> borrowInfoList = borrowInfoService.selectList();
        return R.ok().data("borrowInfoList",borrowInfoList);
    }
    @ApiOperation("借款详情")
    @GetMapping("show/{id}")
    public R show(@ApiParam(value = "借款信息id",required = true) @PathVariable  Long id ){
        Map<String,Object> borrowerInfoDetail = borrowInfoService.getBorrowerInfoDetail(id);
        return R.ok().data("borrowerInfoDetail",borrowerInfoDetail);

    }
    @PostMapping("/approval")
    public R approval(@RequestBody BorrowInfoApprovalVO borrowInfoApprovalVO){
        borrowInfoService.approve(borrowInfoApprovalVO);
        return R.ok().message("审批完成");
    }
}

